package com.wude.guild.manager;

import android.app.Activity;
import android.content.Intent;
import android.widget.CompoundButton;

import com.mc.developmentkit.utils.ToastUtil;
import com.wude.bean.SettingInfo;
import com.wude.guild.R;
import com.wude.tools.DbUtils;
import com.wude.tools.Utils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 * 设置页面开关
 * Created by Administrator on 2017/3/15.
 */

public class CheckedChangeLis implements CompoundButton.OnCheckedChangeListener {

    private final Activity activity;
    private final DbManager db;

    public CheckedChangeLis(Activity activity) {
        this.activity = activity;
        db = DbUtils.getDB();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        try {
            SettingInfo settingBean = Utils.getSettingBean();
            if (settingBean == null){
                ToastUtil.showToast("settingBean等于null");
                return;
            }
            switch (buttonView.getId()) {
                case R.id.wangluo:
                    NetworkChage(isChecked, settingBean);
                    break;
                case R.id.delete:
                    Delete(isChecked, settingBean);
                    break;
                case R.id.anzhuang:
                    Install(isChecked, settingBean);
                    break;
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    /**
     * 是否弹出自动安装
     * @param isChecked
     * @param settingBean
     */
    private void Install(boolean isChecked, SettingInfo settingBean) throws DbException {
        if(isChecked){
            settingBean.tan = 1;
            db.saveOrUpdate(settingBean);
        }else{
            settingBean.tan = 2;
            db.saveOrUpdate(settingBean);
        }
    }

    /**
     * 是否自动删除安装包
     */
    private void Delete(boolean isChecked, SettingInfo settingBean) throws DbException {
        if(isChecked){
            settingBean.delete = 1;
            db.saveOrUpdate(settingBean);
        }else{
            settingBean.delete = 2;
            db.saveOrUpdate(settingBean);
        }
    }

    /**
     * 是否开启网络变化监听
     *
     * @param isChecked
     * @param settingBean
     */
    private void NetworkChage(boolean isChecked, SettingInfo settingBean) throws DbException {
        if (isChecked) {
            Intent intent = new Intent(activity, NetworkService.class);
            activity.startService(intent);
            settingBean.wlti = 1;
            db.saveOrUpdate(settingBean);
        } else {
            Intent intent = new Intent(activity, NetworkService.class);
            activity.stopService(intent);
            settingBean.wlti = 2;
            db.saveOrUpdate(settingBean);
        }
    }
}
